<?php
// +----------------------------------------------------------------------
// | 卡券
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------
namespace app\controller\api\v3;

use app\model\app\ExchangeCoupon as ExchangeCouponModel;
use app\model\app\ExchangeCouponTab as ExchangeCouponTabModel;
use app\model\app\ExchangeCouponGroup as ExchangeCouponGroupModel;
use app\event\app\ExchangeCoupon as ExchangeCouponEvent;
use app\model\app\Modular as ModularModel;

class Yqd extends Common
{


    /**
     * 优惠券主页
     */
    public function gets()
    {
        $data['modular'] =  ModularModel::with('cover')->where('id', 13)->find();
        $data['tab'] = ExchangeCouponTabModel::where('status',1)->select();
        $catalog_name = ExchangeCouponModel::where('status', 1)
            ->where('is_sale',1)
            ->where('back_price','>',0)
            ->whereRaw('`sale_price` >= 3 OR `id` = 638')
            ->column('catalog_name');
        $data['group'] = ExchangeCouponGroupModel::with('cover')
            ->where('name', 'IN', $catalog_name)
            ->where('status',1)->select();
//        $data['group'] = ExchangeCouponGroupModel::with('cover')
//
//            ->where('name', 'IN', function ($query) {
//                $query->name('exchange_coupon')->where('status', 1)
//                    ->where('is_sale',1)
//                    ->where('back_price','>',0)
//                    ->whereRaw('`sale_price` >= 3 OR `id` = 638')
//                    ->field('catalog_name');
//                    //->where('catalog_name',$this->request->param('group_name'));
//            })
//            ->where('status',1)->select();
// 去除没有的
        if ($this->request->param('group_name'))
        {
            $data['vips'] =  ExchangeCouponModel::where('status',1)->where('catalog_name',$this->request->param('group_name'))
                ->distinct(true)
                ->whereRaw('`sale_price` >= 3 OR `id` = 638')
                ->where('back_price','>',0)
                ->where('is_sale',1)
                ->field('name_type')->column('name_type');
            $data['coupons'] = ExchangeCouponModel::with('body')->where('status',1)
                ->where('is_sale',1)
                ->where('back_price','>',0)
                ->whereRaw('`sale_price` >= 3 OR `id` = 638')
                ->where('catalog_name',$this->request->param('group_name'))->select();
        }else{
            $data['vips'] =  ExchangeCouponModel::where('status',1)->where('is_sale',1)
                ->where('back_price','>',0)
//                ->where('sale_price','>=',3)
                ->whereRaw('`sale_price` >= 3 OR `id` = 638')
                ->where('catalog_name',$data['group'][0]['name'])->distinct(true)->field('name_type')->column('name_type');
            $data['coupons'] = ExchangeCouponModel::with('body')->where('is_sale',1)
                ->where('back_price','>',0)
//                ->where('sale_price','>=',3)
                ->whereRaw('`sale_price` >= 3 OR `id` = 638')
                ->where('status',1)->where('catalog_name',$data['group'][0]['name'])->select();
        }
        return $this->successAjax($data);
    }



    public function grp()
    {
//        $names =  ExchangeCouponModel::where('status',1)
//            ->where('is_sale',1)->select();
//
//        foreach ($names as $name) {
//
////            $u['name_type'] =   str_replace(["\r\n", "\n", "\r"], "", nl2br($name['name_type']));;  //strip_tags($name['name_type']);
////            $u['time_name'] =   str_replace(["\r\n", "\n", "\r"], "", nl2br($name['time_name']));;  //strip_tags($name['name_type']);
////            $u['top_name'] =   str_replace(["\r\n", "\n", "\r"], "", nl2br($name['top_name']));;  //strip_tags($name['name_type']);
//            $u['name_type'] =  trim($name['name_type']);  //strip_tags($name['name_type']);
//            $u['time_name'] =  trim($name['name_type']);
//            $u['top_name'] =   trim($name['name_type']);
//            ExchangeCouponModel::where('id',$name['id'])->update($u);
//
//
//        }


        $name_type =  ExchangeCouponModel::where('status',1)
            ->where('is_sale',1) ->distinct(true)
            ->field('name_type')->column('name_type');

        dump($name_type);
    }

    /**
     * 优惠券主页
     */
    public function create()
    {
        $event = new ExchangeCouponEvent();
        $data = $event->order($this->request->param(),$this->user);
        return $this->autoAjax($data);
    }


}
